import AudioMgr from "../common/AudioMgr";
import GameConfig, { GameConfigData } from "../common/GameConfig";
import { HttpService } from "../common/Http";
import { Md5 } from "../common/Md5_0";

const { ccclass, property } = cc._decorator;
export let loadings: loading
@ccclass
export default class loading extends cc.Component {
    @property(cc.Node)
    nodeJiaZai: cc.Node = null;
    @property(cc.Node)
    nodeLogin: cc.Node = null;
    @property(cc.Label)
    txtPro: cc.Label = null;
    @property(cc.Sprite)
    spLoading: cc.Sprite = null;
    @property(cc.Node)
    nodeTitle: cc.Node = null;
    isLoadingOver = false;
    @property(cc.Label)
    txtHint: cc.Label = null;

    onLoad() {
        loadings = this;
        // this.nodeLogin.active = false
        this.initCommon();
        cc.director.preloadScene("GameMain", (cta, total) => {
            let diff = Number((cta / total).toFixed(2))
            if(Math.round(diff * 100) === 98){
                this.isLoadingOver = true;
                // this.nodeJiaZai.active = false;
    
                // let token = cc.sys.localStorage.getItem("dyqlb_token");
                let nickname = cc.sys.localStorage.getItem("userName");
                let avatar = cc.sys.localStorage.getItem("avatar");
                let token = "WVhDVm1pUlV6WXZGcmFPQzNrVC0wanYxd0ZzMzlIaThmaEZ0YXJ5MnVxUjI1bD1SSlVjd3VJTFktU21wY2hGdGFyeTJ1cVIyNWw9UkpVY3d1SUxZQVRtQmlSeTBxZHZKN2I9VkJZZ0pHWDdaLVZmSXdCejB5VlN3dHZqS3RvekcyWkJhWW90aGlQOGcxU3dMOA%3D%3D";
                //本地是否有token
                if (token) {
                    console.log("token ================>", token);
                    let call = ()=>{
                        console.log("get game data over")
                        this.spLoading.fillRange = 1;
                        this.scheduleOnce(()=>{
                            cc.director.loadScene("GameMain")
                        },0.2)
                    }
                    GameConfigData.token = token;
                    GameConfig.prototype.init(call.bind(this));
                    GameConfigData.userName = nickname;
                    // this.getUserHeadByWechatHeadURL(avatar);
                }
                else {
                    // this.nodeLogin.active = true;
                }
            }
            else
            {
                this.txtPro.string = Math.round(diff * 100) + "%"
                this.spLoading.fillRange = diff;
            }
        }, () => {
            console.log("load scene over")
        });
    }

    initCommon() {
        HttpService.getInst();
        Md5.prototype.initMD5();//初始化md5;
        // AudioMgr.init();
        // this.getPublicKey()
        // this.getMd5Key()
    }

    // showTitleAnim(info) {
    //     this.txtHint.string = info;
    //     cc.tween(this.nodeTitle)
    //         .to(1.2, { y: 0, opacity: 255 })
    //         .to(0.6, { y: 80 })
    //         .call(() => {
    //             this.nodeTitle.opacity = 0;
    //             this.nodeTitle.y = -180;
    //         })
    //         .start()
    // }

    // getMd5Key() {
    //     let callback = (msg) => {
    //         GameConfigData.md5_key = msg;
    //     }
    //     utils.adManager.md5Key(callback.bind(this));
    // }

    // getPublicKey() {
    //     let callback = (msg) => {
    //         GameConfigData.public_key = msg;
    //     }
    //     utils.adManager.getSmallPublicKey(callback.bind(this))
    // }


    // onButtonLoginCallback(event, coustom) {
    //     audioMgrs.playGameAudio("buttonCheck");
    //     let callback = (res, msg) => {
    //         if (res) {
    //             console.log("token ================>", msg.data.token);
    //             GameConfigData.token = msg.data.token;
    //             GameConfigData.userName = msg.data.nickname;
    //             this.getUserHeadByWechatHeadURL(msg.data.avatar);
    //             cc.sys.localStorage.setItem("dyqlb_token", msg.data.token)
    //             cc.sys.localStorage.setItem("userName", msg.data.nickname)
    //             cc.sys.localStorage.setItem("avatar", msg.data.avatar);
    //             gameConfig.prototype.init();
    //         }
    //         else {
    //             this.showTitleAnim("请求超时，请检查您的网络连接状态!!!");
    //         }
    //     }
    //     utils.adManager.WxloginCallback(callback.bind(this));
    // }

    // getUserHeadByWechatHeadURL(url) {
    //     cc.loader.load({ url: url, type: "jpg" }, (err, tex) => {
    //         if (err) {
    //             return
    //         }
    //         //@ts-ignore
    //         GameConfigData.userHead = new cc.SpriteFrame(tex);
    //     });
    // }
}

/**
 * 当前主屋，厨房，货摊，道路，鱼塘等级
 * 主屋，厨房，货摊，道路，鱼塘等级升级配置
 * 当前任务
 * 红包数据
 * 提现次数
 * 是否新手
 * 登录天数
 * 钓鱼数据[{类型,时间状态}] -1未解锁 0解锁 1<= 不同鱼的类型
 * 钓鱼时间配置
 * 红包券
 * 钞票
 * 订单数据
 * 全体加速次数
 * 是否连续打卡
 * 存钱罐数据
 * 看广告次数
 * 离线时间
 * 厨房食材加工数据
 * 食材加工时间配置
 */
